package mall.demo.servlet;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mall.demo.mybatis.pojo.Admin;
import mall.demo.mybatis.pojo.Order;
import mall.demo.mybatis.service.OrderService;
import mall.demo.util.OrderStatusEnum;
import mall.demo.util.OrderUtil;
import mall.demo.util.SessionKeyEnum;

/**
 * Servlet implementation class UpdateOrderStatusServlet
 */
@WebServlet("/servlet/updateOrderStatus.do")
public class UpdateOrderStatusServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateOrderStatusServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		Admin admin = (Admin)request.getSession().getAttribute(SessionKeyEnum.adminObj.name());
		if(admin == null) {
			response.sendRedirect(request.getContextPath() + "/admin/login.jsp");
			return;
		}
		String oid = request.getParameter("oid");
		Order order = (new OrderService()).getOrderById(Integer.parseInt(oid));
		if (order == null) {
			response.sendRedirect(request.getContextPath() + "/admin/order.jsp");
			return;
		}
		OrderStatusEnum newSt = OrderUtil.nextOrderStatus(
				OrderStatusEnum.valueOf(order.getStatus()));
		order.setStatus(newSt.name());
		order.setLastTime(new Date());
		(new OrderService()).updateOrderStatus(order);
		response.sendRedirect(request.getContextPath() + "/admin/order.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
